﻿using System;
using System.Collections.Generic;
using System.Linq;
using MRGSP.SurveyMaster.Core;
using MRGSP.SurveyMaster.Core.Model;
using MRGSP.SurveyMaster.Service.Utils;
using NUnit.Framework;

namespace MRGSP.SurveyMaster.Tests
{
    [TestFixture]
    public class SurveyTests
    {
        private IEnumerable<Question> source;

        [SetUp]
        public void SetUp()
        {
            source = new[] 
                         { 
                             new Question {Id = 1, Text = "question1", Type = ControlType.Header}, 
                             new Question { Id = 2, Text = "question2", Type = ControlType.Header}, 
                             new Question { Id = 3, Text = "question3", Type = ControlType.Grid}, 

                             new Question { Id = 4, ParentId = 1, Text = "item1", Type = ControlType.Checkbox}, 
                             new Question { Id = 5, ParentId = 1, Text = "item2", Type = ControlType.Checkbox}, 
                             new Question { Id = 6, ParentId = 1, Text = "item3", Type = ControlType.Checkbox}, 

                             new Question { Id = 7, ParentId = 6, Text = " explain why", Type = ControlType.Textbox}, 

                             new Question { Id = 8, ParentId = 2, Text = "item1 for q2", Type = ControlType.Radio},
                             new Question { Id = 9, ParentId = 2, Text = "item2 for q2", Type = ControlType.Radio},

                             new Question { Id = 10, ParentId = 3, Text = "Word", Type = ControlType.Checkbox},
                             new Question { Id = 11, ParentId = 3, Text = "Excel", Type = ControlType.Checkbox},
                             new Question { Id = 12, ParentId = 3, Text = "Outlook", Type = ControlType.Checkbox},
                
                             new Question { Id = 13, ParentId = 10, Text = "1", Type = ControlType.Radio},
                             new Question { Id = 14, ParentId = 10, Text = "2", Type = ControlType.Radio},
                             new Question { Id = 15, ParentId = 10, Text = "3", Type = ControlType.Radio},
                             new Question { Id = 101, ParentId = 10, Type = ControlType.Textbox, Text = "Uber Text"},
                
                             new Question { Id = 16, ParentId = 11, Text = "1", Type = ControlType.Radio},
                             new Question { Id = 17, ParentId = 11, Text = "2", Type = ControlType.Radio},
                             new Question { Id = 18, ParentId = 11, Text = "3", Type = ControlType.Radio},

                             new Question { Id = 19, ParentId = 12, Text = "1", Type = ControlType.Radio},
                             new Question { Id = 20, ParentId = 12, Text = "2", Type = ControlType.Radio},
                             new Question { Id = 21, ParentId = 12, Text = "3", Type = ControlType.Radio},

                             new Question {Id = 22, Text = "question4", Type=ControlType.Grid},
                             new Question {Id = 23, Text = "yes", ParentId = 22, Type = ControlType.Checkbox},
                             new Question {Id = 24, Text = "no", ParentId = 22, Type = ControlType.Checkbox},

                             new Question { Id = 25, ParentId = 23, Type = ControlType.Textbox},
                             new Question { Id = 26, ParentId = 23, Type = ControlType.Textbox},
                             new Question { Id = 27, ParentId = 23, Type = ControlType.Textbox},

                             new Question { Id = 28, ParentId = 24, Type = ControlType.Textbox},
                             new Question { Id = 29, ParentId = 24, Type = ControlType.Textbox},
                             new Question { Id = 30, ParentId = 24, Type = ControlType.Textbox},

                             new Question { Id = 31, Text = "uber question", Type=ControlType.Header},

                             new Question { Id = 33, ParentId = 31, Text = "item1", Type = ControlType.Checkbox},
                             new Question { Id = 34, ParentId = 31, Text = "item2", Type = ControlType.Checkbox},
                             new Question { Id = 35, ParentId = 31, Text = "Other", Type = ControlType.Textbox},

                             new Question { Id = 36, ParentId = 33, Text = "cuz I do", Type = ControlType.Radio},
                             new Question { Id = 37, ParentId = 33, Text = "cuz I dont", Type = ControlType.Radio},
                             new Question { Id = 38, ParentId = 33, Text = " explain", Type = ControlType.Textbox},
                
                             new Question { Id = 39, ParentId = 35, Text = "first one", Type = ControlType.Checkbox},
                             new Question { Id = 40, ParentId = 35, Text = "second one", Type = ControlType.Checkbox},
                             new Question { Id = 41, ParentId = 35, Text = "something else", Type = ControlType.Checkbox},
                             new Question { Id = 42, ParentId = 35, Text = " explain some more", Type = ControlType.Textbox},

                             new Question { Id = 43, ParentId = 34, Text = " mark your skill:", Type = ControlType.Header},

                             new Question { Id = 44, ParentId = 43, Text = "good", Type = ControlType.Radio},
                             new Question { Id = 45, ParentId = 43, Text = "super good", Type = ControlType.Radio},
                             new Question { Id = 46, ParentId = 43, Text = "uber", Type = ControlType.Radio},

                             new Question { Id = 47, ParentId = 46, Text = " why uber ? ", Type=ControlType.Textbox}
                
                         };
        }

        [Test]
        public void BuildsTree()
        {
            var result = new TreeBuilder().Build(source);
            Assert.AreEqual(5, result.Count());
            Assert.AreEqual(3, result.First().Children.Count());
            Assert.AreEqual(2, result.ToArray()[1].Children.Count());
        }

        [Test]
        public void GeneratesHtml()
        {
            var result = new SurveyCreateBuilder(new TreeBuilder()).Build(source);
            Console.Out.Write(result);
        }

        [Test]
        public void Aa()
        {
            var x = new TreeBuilder().Build(new[]
                                        {
                                            new Question {Id = 1,},
                                            new Question {Id = 2, ParentId = 1},
                                        });
        }


        [Test]
        public void EnumerableTest()
        {
            var source = GetFoos().ToList();
            var source1 = new[]
                             {
                                 new Foo(){One ="1", Two = "2", Three = true},
                                 new Foo(){One ="1", Two = "2", Three = true},
                                 new Foo(){One ="1", Two = "2", Three = true},
                             }.AsEnumerable();

            source.First().One = "hello";

            source.First().One.IsEqualTo("hello");

        }

        public IEnumerable<Foo> GetFoos()
        {
            yield return new Foo() {One = "1", Two = "2", Three = true};
            yield return new Foo() {One = "1", Two = "2", Three = true};
            yield return new Foo() {One = "1", Two = "2", Three = true};
        }

        public class Foo
        {
            public string One { get; set; }
            public string Two { get; set; }
            public bool Three { get; set; }
        }



    }
}